import grails.converters.JSON;

import java.util.Date;

import org.codehaus.groovy.grails.commons.ApplicationHolder;

import com.axon.lemon.*;

class BootStrap {
	
	/*
	 1.  role_acc(done)
	 2.  property_type(done)
	 3.  city(done)
	 4.  district(done)
	 5.  feature(done)
	 6.  favourite(done)
	 7.  user_profile(done)
	 8.  contact(done)
	 9.  image
	 10. place(done)
	 11. notification
	 12. comment(done)
	 13. like_number(done)
	 */
	
    def init = { servletContext ->
		/*RoleAcc*/
		def grailsApplication
		RoleAcc adminrole = new RoleAcc(nameRole: "admin").save(flush: true)
		RoleAcc userrole  = new RoleAcc(nameRole: "user").save(flush: true)
		//================================================
		
		/*UserProfile*/
		def usr1 = new UserProfile(email: "nguyenlv@axonactive.vn", fullName: "Le Vu Nguyen", gender: "Male", company: "AxonActive VN", website: "http://www.nguyenlv.wordpress.com", password: "123456")
		def usr2 = new UserProfile(email: "lechau2004@gmail.com", fullName: "Le Thi Minh Chau", gender: "Female", company: "No Company", website: "http://www.minhchau.wordpress.com", password: "123456")
		def usr3 = new UserProfile(email: "nganha08t2@gmail.com", fullName: "Phan Thi Ngan Ha", gender: "Female", company: "No Company", website: "http://www.nganha.wordpress.com", password: "123456")
		def usr4 = new UserProfile(email: "hieu.nguyen228@gmail.com", fullName: "Nguyen Hieu", gender: "Male", company: "No Company", website: "http://www.nguyenhieu.wordpress.com", password: "123456")
		def usr5 = new UserProfile(email: "nguyenlx@outlook.com", fullName: "Le Xuan Nguyen", gender: "Male", company: "No Company", website: "http://www.nguyenlx.wordpress.com", password: "123456")
		def usr6 = new UserProfile(email: "quangr1@gmail.com", fullName: "Nguyen Quang", gender: "Male", company: "No Company", website: "http://www.nguyenquang.wordpress.com", password: "123456")
		usr1.roleacc=adminrole
		usr1.save()
		usr2.roleacc=userrole
		usr2.save()
		usr3.roleacc=userrole
		usr3.save()
		usr4.roleacc=userrole
		usr4.save()
		usr5.roleacc=userrole
		usr5.save()
		usr6.roleacc=userrole
		usr6.save()
		//================================================
		
		/*City*/
		def danang = new City(cityName: "Da Nang", zipCode: "59000")
		def hn = new City(cityName: "Ha Noi", zipCode: "10000")
		//================================================
		
		/*District*/
		def haichau = new District(districtName: "Hai Chau")
		def thanhkhe = new District(districtName: "Thanh Khe")
		def lienchieu = new District(districtName: "Lien Chieu")
		def nguhanhson = new District(districtName: "Ngu Hanh Son")
		def sontra = new District(districtName: "Son Tra")
		
		haichau.save(flush:true)
		thanhkhe.save(flush:true)
		lienchieu.save(flush:true)
		nguhanhson.save(flush:true)
		sontra.save(flush:true)
		
		haichau.city=danang
		thanhkhe.city=danang
		lienchieu.city=danang
		nguhanhson.city=danang
		sontra.city=danang
		
		danang.save(flush:true)
		hn.save(flush:true)
		//================================================
		
		/*Feature*/
		Feature f1 = new Feature(nameFeature: "air").save(flush: true)
		Feature f2 = new Feature(nameFeature: "celling").save(flush: true)
		Feature f3 = new Feature(nameFeature: "elevator").save(flush: true)
		Feature f4 = new Feature(nameFeature: "laundry").save(flush: true)
		Feature f5 = new Feature(nameFeature: "parking").save(flush: true)
		Feature f6 = new Feature(nameFeature: "garage").save(flush: true)
		//================================================
		
		/*PropertyType*/
		PropertyType p1 = new PropertyType(namePropertyType:"house").save(flush: true)
		PropertyType p2 = new PropertyType(namePropertyType:"land").save(flush: true)
		PropertyType p3 = new PropertyType(namePropertyType:"apartment").save(flush: true)
		PropertyType p4 = new PropertyType(namePropertyType:"apartment building").save(flush: true)
		PropertyType p5 = new PropertyType(namePropertyType:"villa").save(flush: true)
		PropertyType p6 = new PropertyType(namePropertyType:"codominium").save(flush: true)
		//================================================
		
		
		//${resource(dir: 'images', file: 'apple-touch-icon-retina.png')}
		String rpath1 = ApplicationHolder.application.parentContext.servletContext.getRealPath("/images/avatar/1.jpg")
		File file1 = new File(rpath1)
		
		String rpath2 = ApplicationHolder.application.parentContext.servletContext.getRealPath("/images/avatar/2.jpg")
		File file2 = new File(rpath2)
		
		String rpath3 = ApplicationHolder.application.parentContext.servletContext.getRealPath("/images/avatar/3.jpg")
		File file3 = new File(rpath3)
		
		String rpath4 = ApplicationHolder.application.parentContext.servletContext.getRealPath("/images/avatar/4.jpg")
		File file4 = new File(rpath4)
		
		String rpath5 = ApplicationHolder.application.parentContext.servletContext.getRealPath("/images/avatar/5.jpg")
		File file5 = new File(rpath5)
		
		byte[] imgByte1 = file1.getBytes()
		byte[] imgByte2 = file2.getBytes()
		byte[] imgByte3 = file3.getBytes()
		byte[] imgByte4 = file4.getBytes()
		byte[] imgByte5 = file5.getBytes()
		
		/*Place*/
		def place1 = new Place(lat:"16.063527", lng:"108.224182",price:"200",address:"238 Bach Dang",description:"At Green Plaza Hotel, a spacious Vietnamese-inspired room with a wide, elegant bed serves as your temporary .",buyOrRent: "true",bed:"2",bath:"1",status:true)
		place1.propertytype=p3
		place1.district=haichau
		place1.city=danang
		place1.userprofile=usr5
		place1.avatar=imgByte1
		
			
		def place2 = new Place(lat:"16.066991", lng:"108.223765",price:"250",address:"177 Tran Phu",description:"Bamboo Green Hotel is located right in the heart of Danang. The hotel is in close proximity to the business district",buyOrRent: "true",bed:"2",bath:"1",status:true)
		place2.propertytype=p3
		place2.district=thanhkhe
		place2.city=danang
		place2.userprofile=usr4
		place2.avatar=imgByte2
		place2.save(flush: true)
		
		def place3 = new Place(lat:"16.069032", lng:"108.217092",price:"300",address:"40 Ngo Gia Tu",description:"Louis Hotel also offers many facilities to enrich your stay in Danang. For the comfort and convenience of guests.",buyOrRent: "true",bed:"2",bath:"1",status:true)
		place3.propertytype=p3
		place3.district=sontra
		place3.city=danang
		place3.userprofile=usr3
		place3.avatar=imgByte3
		place3.save(flush: true)
		
		def place4 = new Place(lat:"16.071238", lng:"108.215225",price:"180",address:"136 Le Duan",description:"The hotel is located opposite the Chi Lang Stadium and is 1.5 kilometers from Da Nang International Airport.",buyOrRent: "true",bed:"3",bath:"2",status:true)
		place4.propertytype=p3
		place4.district=thanhkhe
		place4.city=danang
		place4.userprofile=usr5
		place4.avatar=imgByte4
		place4.save(flush: true)
		
		def place5 = new Place(lat:"16.067733", lng:"108.223829",price:"220",address:"155 Tran Phu",description:"DAESCO Hotel is an 11 storey complex which is situated in the heart of Danang.",buyOrRent: "true",bed:"2",bath:"2",status:true)
		place5.propertytype=p3
		place5.district=haichau
		place5.city=danang
		place5.userprofile=usr4
		place5.avatar=imgByte5
		place5.save(flush: true)
		//================================================
		
		/*Contact*/
		def c1 = new Contact(nameContact:"Le Xuan Nguyen",phoneContact:"0988164054",emailContact:"nguyenlx90@gmail.com").save()
		place1.contact = c1
		place1.save(flush: true)
		
		def c2 = new Contact(nameContact:"Le Thi Minh Chau",phoneContact:"0974471639",emailContact:"lechau2004@gmail.com").save()
		place2.contact = c2
		place2.save(flush: true)
		
		def c3 = new Contact(nameContact:"Phan Thi Ngan Ha",phoneContact:"0974471852",emailContact:"nganha@gmail.com").save()
		place3.contact = c3
		place3.save(flush: true)
		
		def c4 = new Contact(nameContact:"Nguyen Quang",phoneContact:"0974471147",emailContact:"nguyenquang@gmail.com").save()
		place4.contact = c4
		place4.save(flush: true)
		
		def c5 = new Contact(nameContact:"Nguyen Hieu",phoneContact:"0974471123",emailContact:"nguyenhieu@gmail.com").save()
		place5.contact = c5
		place5.save(flush: true)
		
		//================================================
		/*Image*/
		
		String rpath11 = ApplicationHolder.application.parentContext.servletContext.getRealPath("/images/gallery/11.jpg")
		File f11 = new File(rpath11);
		byte[] byteimg11 = f11.getBytes()
		Image img11 = new Image()
		img11.path = byteimg11
		img11.place = place2
		img11.save(flush:true)
		
		String rpath12 = ApplicationHolder.application.parentContext.servletContext.getRealPath("/images/gallery/12.jpg")
		File f12 = new File(rpath12);
		byte[] byteimg12 = f12.getBytes()
		Image img12 = new Image()
		img12.path = byteimg12
		img12.place = place2
		img12.save(flush:true)
		
		String rpath13 = ApplicationHolder.application.parentContext.servletContext.getRealPath("/images/gallery/13.jpg")
		File f13 = new File(rpath13);
		byte[] byteimg13 = f13.getBytes()
		Image img13 = new Image()
		img13.path = byteimg13
		img13.place = place2
		img13.save(flush:true)
		
		String rpath14 = ApplicationHolder.application.parentContext.servletContext.getRealPath("/images/gallery/21.jpg")
		File f14 = new File(rpath14);
		byte[] byteimg14 = f14.getBytes()
		Image img14 = new Image()
		img14.path = byteimg14
		img14.place = place1
		img14.save(flush:true)
			
		String rpath15 = ApplicationHolder.application.parentContext.servletContext.getRealPath("/images/gallery/22.jpg")
		File f15 = new File(rpath15);
		byte[] byteimg15 = f15.getBytes()
		Image img15 = new Image()
		img15.path = byteimg15
		img15.place = place1
		img15.save(flush:true)
		
		String rpath16 = ApplicationHolder.application.parentContext.servletContext.getRealPath("/images/gallery/23.jpg")
		File f16 = new File(rpath16);
		byte[] byteimg16 = f16.getBytes()
		Image img16 = new Image()
		img16.path = byteimg16
		img16.place = place1
		img16.save(flush:true)
		
		String rpath17 = ApplicationHolder.application.parentContext.servletContext.getRealPath("/images/gallery/31.jpg")
		File f17 = new File(rpath17);
		byte[] byteimg17 = f17.getBytes()
			Image img17 = new Image()
		img17.path = byteimg17
		img17.place = place3
		img17.save(flush:true)
			
		String rpath18 = ApplicationHolder.application.parentContext.servletContext.getRealPath("/images/gallery/32.jpg")
		File f18 = new File(rpath18);
		byte[] byteimg18 = f18.getBytes()
		Image img18 = new Image()
		img18.path = byteimg18
		img18.place = place3
		img18.save(flush:true)
		
		String rpath19 = ApplicationHolder.application.parentContext.servletContext.getRealPath("/images/gallery/33.jpg")
		File f19 = new File(rpath19);
		byte[] byteimg19 = f19.getBytes()
		Image img19 = new Image()
		img19.path = byteimg19
		img19.place = place3
		img19.save(flush:true)
		
		String rpath20 = ApplicationHolder.application.parentContext.servletContext.getRealPath("/images/gallery/41.jpg")
		File f20 = new File(rpath20);
		byte[] byteimg20 = f20.getBytes()
		Image img20 = new Image()
		img20.path = byteimg20
		img20.place = place4
		img20.save(flush:true)
		//println(img11.validate())
		
		String rpath21 = ApplicationHolder.application.parentContext.servletContext.getRealPath("/images/gallery/42.jpg")
		File f21 = new File(rpath21);
		byte[] byteimg21 = f21.getBytes()
		Image img21 = new Image()
		img21.path = byteimg21
		img21.place = place4
		img21.save(flush:true)
		
		String rpath22 = ApplicationHolder.application.parentContext.servletContext.getRealPath("/images/gallery/43.jpg")
		File f22 = new File(rpath22);
		byte[] byteimg22 = f22.getBytes()
		Image img22 = new Image()
		img22.path = byteimg22
		img22.place = place4
		img22.save(flush:true)
		
		String rpath23 = ApplicationHolder.application.parentContext.servletContext.getRealPath("/images/gallery/51.jpg")
		File f23 = new File(rpath23);
		byte[] byteimg23 = f23.getBytes()
		Image img23 = new Image()
		img23.path = byteimg23
		img23.place = place5
		img23.save(flush:true)
		
		String rpath24 = ApplicationHolder.application.parentContext.servletContext.getRealPath("/images/gallery/52.jpg")
		File f24 = new File(rpath24);
		byte[] byteimg24 = f24.getBytes()
		Image img24 = new Image()
		img24.path = byteimg24
		img24.place = place5
		img24.save(flush:true)
		
		String rpath25 = ApplicationHolder.application.parentContext.servletContext.getRealPath("/images/gallery/53.jpg")
		File f25 = new File(rpath25);
		byte[] byteimg25 = f25.getBytes()
		Image img25 = new Image()
		img25.path = byteimg25
		img25.place = place5
		img25.save(flush:true)
		
		//================================================
		
		/*Comment*/
		def cm1 = new Comment(content: "Too cheap")
		cm1.userprofile=usr1
		cm1.place=place1
		cm1.save(flush:true)
		//================================================
		
		/*LikeNumber*/
		def like1 = new LikeNumber()
		like1.userprofile=usr2
		like1.place=place1
		like1.save(flush:true)
		//================================================
		
		/*Favourite*/
		def fa1 = new Favourite()
		fa1.userprofile=usr4
		fa1.place=place1
		fa1.save(flush:true)
		
		def fa2 = new Favourite()
		fa2.userprofile=usr3
		fa2.place=place1
		fa2.save(flush:true)
		
		JSON.registerObjectMarshaller(Comment) {
			def returnArray = [:]
			  returnArray['content'] = it.content
			  returnArray['userprofile'] = it.userprofile
			return returnArray
		}
    }
    def destroy = {
    }
}








/*
	def jdoe = new User(login:"jdoe", password:"password", name:"John Doe")
    def e1 = new Entry(title:"Grails 1.1 beta is out", summary:"Check out the new features")
    def e2 = new Entry(title:"Just Released - Groovy 1.6 beta 2", summary:"It is looking good.")
    jdoe.addToEntries(e1)
    jdoe.addToEntries(e2)
    jdoe.save()
    
    def jsmith = new User(login:"jsmith", password:"wordpass", name:"Jane Smith")
    def e3 = new Entry(title:"Codecs in Grails", summary:"See Mastering Grails")
    def e4 = new Entry(title:"Testing with Groovy", summary:"See Practically Groovy")
    jsmith.addToEntries(e3)
    jsmith.addToEntries(e4)
    jsmith.save()
*/

/*
 class BootStrap {
  def init = { servletContext ->
    def writer = new Author(name:"Jane")
    writer.save()
    if(writer.hasErrors()){
       println writer.errors
    }
    def comic = new Book(name: "superman", Author: writer)
    comic.save()
    if(comic.hasErrors()){
       println comic.errors
    }
  }
def destroy = {}
}*
def a = new Author(name: "Stephen King")
             .addToFiction(title: "IT")
             .addToNonFiction(title: "On Writing: A Memoir of the Craft")
             .save()
*/
/*
 Book myBook=new Book(name:'MyBook', publication:'lorem',price:'200')
 Author author1=new Author(name:'Partho',country:'India')
 author.addToBooks(myBook)
 */